﻿using System.Collections.Generic;
using UFIDA.U9.WSS.ProxyService.AccessProxy.Configuration;
using UFSoft.UBF.Util.Log;

namespace UFIDA.U9.WSS.ProxyService.AccessProxy
{
    /// <summary>
    ///     授权代理管理类
    /// </summary>
    public sealed class AccessProxyManager
    {
        private static AccessProxyManager _instance;
        private static readonly object Lock = new object();
        private static readonly object InitLock = new object();
        private static readonly ILogger Logger = LoggerManager.GetLogger(typeof(AccessProxyManager));
        private Dictionary<string, List<string>> _accessProxyDict;
        private bool _isInited;

        private AccessProxyManager()
        {
        }

        public static AccessProxyManager Instance
        {
            get
            {
                if (_instance != null)
                {
                    if (!_instance._isInited)
                    {
                        _instance.Init();
                    }

                    return _instance;
                }

                lock (Lock)
                {
                    if (_instance == null)
                    {
                        _instance = new AccessProxyManager();
                        _instance.Init();
                    }
                    else if (!_instance._isInited)
                    {
                        _instance.Init();
                    }

                    return _instance;
                }
            }
        }

        /// <summary>
        ///     是否允许访问
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="proxyType"></param>
        /// <returns></returns>
        public bool IsAccess(string groupName, string proxyType)
        {
            if (string.IsNullOrEmpty(groupName) || string.IsNullOrEmpty(proxyType)) return false;
            string groupNameLower = groupName.ToLower();
            if (!_accessProxyDict.ContainsKey(groupNameLower)) return false;
            string proxyTypeLower = proxyType.ToLower().Replace(" ", string.Empty);
            if (string.IsNullOrEmpty(proxyTypeLower)) return false;
            List<string> accessProxyList = _accessProxyDict[groupNameLower];
            return accessProxyList.Contains(proxyTypeLower);
        }

        /// <summary>
        ///     初始化
        /// </summary>
        public void Init()
        {
            lock (InitLock)
            {
                if (_isInited) return;
                if (_accessProxyDict != null) return;
                Logger.Debug("AccessProxyManager init...");
                _accessProxyDict = new Dictionary<string, List<string>>();
                AccessProxysSectionGroup accessProxySectionGroup = AccessProxysSectionGroup.GetConfig();
                if (accessProxySectionGroup == null) return;
                foreach (GroupSection groupSection in accessProxySectionGroup.Groups)
                {
                    if (groupSection.Proxys == null || groupSection.Proxys.Count == 0) continue;
                    string groupName = groupSection.Name;
                    if (string.IsNullOrWhiteSpace(groupName)) continue;
                    groupName = groupName.ToLower().Replace(" ", string.Empty);
                    if (string.IsNullOrWhiteSpace(groupName)) continue;
                    List<string> proxyTypeList = new List<string>();
                    _accessProxyDict.Add(groupName, proxyTypeList);
                    foreach (ProxySection ProxySection in groupSection.Proxys)
                    {
                        string proxyType = ProxySection.ProxyType;
                        if (string.IsNullOrWhiteSpace(proxyType)) continue;
                        proxyType = proxyType.ToLower().Replace(" ", string.Empty);
                        if (string.IsNullOrWhiteSpace(proxyType)) continue;
                        if (proxyTypeList.Contains(proxyType)) continue;
                        proxyTypeList.Add(proxyType);
                    }
                }

                _isInited = true;
            }
        }
    }
}